package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.GetStoreInfoBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.QRCode;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.wrapper.GlideImageUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * @author Programming
 * @date 2017/7/3 0003
 */
public class ShopBusinessCardActivity extends BaseSecondActivity implements View.OnClickListener {

    @BindView(R.id.shopbusinesscard_head)
    ExpandHeadView mShopbusinesscardHead;
    @BindView(R.id.storeLogo)
    CircleImageView mStoreLogo;
    @BindView(R.id.myshop_name)
    TextView mMyshopName;
    @BindView(R.id.myshop_location)
    TextView mMyshopLocation;
    @BindView(R.id.viewlocation)
    LinearLayout mViewLocation;
    @BindView(R.id.domainName)
    TextView mDomainName;
    @BindView(R.id.businesstime)
    TextView mBusinesstime;
    @BindView(R.id.contacttel)
    TextView mContacttel;
    @BindView(R.id.shopmemo)
    TextView mShopmemo;
    @BindView(R.id.qrcode)
    ImageView mQrcode;
    public GetStoreInfoBean.StoreInfoBean mStoreInfo;


    @Override
    protected int setContentView() {
        return R.layout.activity_shopbusinesscard;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mShopbusinesscardHead;
    }

    @Override
    protected void initView() {
        initData();
        initEvent();
    }

    private void initData() {
        OkHttpUtils.post().url(Constants.GETSTOREINFO_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("carBusinessId", SPUtils.getString("anotherBusinessId", ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                GetStoreInfoBean getStoreInfoBean = new Gson().fromJson(response, GetStoreInfoBean.class);

                mStoreInfo = getStoreInfoBean.getStoreInfo();
                mMyshopName.setText(mStoreInfo.getStoreName());
                mMyshopLocation.setText(mStoreInfo.getStoreAddress());
                if (TextUtils.isEmpty(mStoreInfo.getStoreLogo())) {
                    mStoreLogo.setImageResource(R.drawable.logo_moren_2x);
                } else {
                    GlideImageUtils.loadImage(Constants.IMAGEURL + mStoreInfo.getStoreLogo(),mStoreLogo);
                }
                String hours = mStoreInfo.getBusinessHours().replace(",", "-");
                mBusinesstime.setText(hours);
                mContacttel.setText(mStoreInfo.getStoreMobile());
                mShopmemo.setText(mStoreInfo.getStoreProfile());
                String domainName = Constants.MYSHOPVISITE_URL + "?storeId=" + mStoreInfo.getId() + "&returnTo=1";
                mDomainName.setText(domainName);
                mQrcode.setImageBitmap(QRCode.createQRCode(domainName));
//                String url=Constants.IMAGEURL + mStoreInfo.getCodeImage();
//                Glide.with(UIUtils.getContext()).load(Constants.IMAGEURL + mStoreInfo.getCodeImage()).into(mQrcode);
            }
        });
    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        mViewLocation.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        //打开集成的地图
        Intent intent = new Intent(UIUtils.getContext(), MapShowActivity.class);
        intent.putExtra("point", mStoreInfo.getStoreLatlon());
        intent.putExtra("allAddress", mStoreInfo.getStoreAddress());
        ALog.d(mStoreInfo.getStoreLatlon());
        startActivity(intent);

    }
}
